import { GetServerSideProps, NextPage, PageConfig } from 'next';
import Head from 'next/head';
import Content from './content';
import { Query as Q1 } from '../api/teamglz/query.api';
import { tbclass, tbteam } from '../../atoms/db';

interface IProps {
	query: Q1;
	id: string;
	teams: Pick<ITbteam, 'team_id' | 'teamname' | 'teammate'>[];
	classs: Pick<ITbclass, 'class_id' | 'classname' | 'grade'>[];
}

/**
 * 团队管理
 */
const Page: NextPage<IProps> = ({ query, id, teams, classs }) => {
	return (
		<>
			<Head>
				<title>团队管理</title>
			</Head>
			<Content initialquery={query} id={id} teams={teams} classs={classs} />
		</>
	);
};

export const config: PageConfig = {
	amp: false
};

export default Page;

// pre-render this page on each request
export const getServerSideProps: GetServerSideProps<IProps> = async (context) => {
	const query = context.query as Record<string, string>;
	const teams = await tbteam().select('*').orderBy('teamname', 'asc');
	const classs = await tbclass().select('*').orderBy('classname', 'asc');
	return {
		props: {
			classs,
			teams,
			query: {
				page: query.page || '1',
				pagesize: '10',
				q: query.q || '',
				id: query.id || ''
			},
			id: query.id || ''
		}
	};
};
